<?php
namespace app\admin\controller\admin\goods;
use app\admin\model\Goods;
use app\admin\model\GoodsClassification;
class GoodsList extends \app\admin\controller\admin\BaseAdmin{
    public function index(){
        //商品查询
        $request = \think\Request::instance();
        //获取关键字的查询条件
        $key = $request->get('key');
        //获取商品状态的查询条件
        $status = $request->get('status');
        //获取商品排序的查询条件
        $orderby = $request->get('orderby');
        //获取商品类别的查询条件
        $category = $request->get('category');
        //获取商品口味的查询条件
        $taste = $request->get('taste');
        //获取商品特色的查询条件
        $features = $request->get('features');
        //获取商品规格的查询条件
        $size = $request->get('size');
        //预制默认查询条件
        $where = [];
        //预制默认排序条件        
        $odb = '';

        //根据商品的类别进行商品查询
        //预制商品类别条件
        $categoryCondition = [
            '1' => '1',
            '2' => '2',
            '3' => '3',
            '4' => '4',
            '5' => '5',
            '6' => '6',
        ];
        if(array_key_exists($category, $categoryCondition) === true){
            $where['category'] = $category;
        }

        //根据商品的口味进行商品查询
        //预制商品口味条件
        $tasteCondition = [
            '1' => '1',
            '2' => '2',
            '3' => '3',
            '4' => '4',
            '5' => '5',
            '6' => '6',
            '7' => '7',
            '8' => '8',
            '9' => '9',
        ];
        if(array_key_exists($taste, $tasteCondition) === true){
            $where['taste'] = $taste;
        }
        //根据商品的特色进行商品查询
        //预制商品特色条件
        $featuresCondition = [
            '1' => '1',
            '2' => '2',
            '3' => '3',
            '4' => '4',
            '5' => '5',
            '6' => '6',
            '7' => '7',
        ];
        if(array_key_exists($features, $featuresCondition) === true){
            $where['features'] = $features;
        }
        //根据商品的规格进行商品查询
        //预制商品规格条件跟商品类别条件一致
        if(array_key_exists($size, $categoryCondition) === true){
            $where['size'] = $size;
        }      
        
        //根据商品状态进行商品查询
        //预制商品状态0代表下架 1代表上架
        $state = [
            '0' => '0',
            '1' => '1',
        ];

        //判断接收的状态值是否在预制商品状态中
        if(array_key_exists($status,$state) === true){
            $where['status'] = $status;
        }else{
            //当状态值为空或者不为预制状态值时，设立默认选项。状态值为2代表商品状态“所有”
            $status = 2;
        }

        //根据商品的排序进行商品查询
        //预制排序条件
        $order = [           
            'price_desc' => 'price desc',
            'price_asc' => 'price asc',
            'stock_number_desc' => 'stock_number desc',
            'stock_number_asc' => 'stock_number asc',
        ];
        //检查排序条件中是否在预制排序条件中有值
        if(array_key_exists($orderby, $order) === true){
            $odb = $order[$orderby];                         
        }else{
            $orderby = 'goods_id_desc';
            $odb = 'a.id desc';                     
        }

        $goods = \think\Db::table('pc_goods')
            ->alias('a')
            ->join('pc_goods_classification w','a.id = w.goods_id')
            ->join('pc_goods_attr c','a.id = c.goods_id')
            ->where('is_delete','0')
            ->where('admin_id',$this->adminData->id)
            ->where($where)
            ->where('title','like',"%{$key}%")
            ->order($odb);
        //查询出所有符合条件的商品
        //做分页功能的时候，保证点击页数之后以get方式传的值不会丢失
        $rows = $goods->paginate(5,false,['query' => $request->get()]);
        $data['rows'] = $rows;
        $data['key'] = $key;
        $data['status'] = $status;
        $data['orderby'] = $orderby;
        $data['category'] = $category;
        $data['taste'] = $taste;
        $data['features'] = $features;
        $data['size'] = $size;
        return view('admin/goods/goodsList',$data);
    }
    /*
    处理商品删除
     */
    public function goodsDel(){
        $request = \think\Request::instance();
        //接收传过来的商品id
        $goods_id = $request->get('goods_id');
        $goods = new Goods();
        //查询该管理员管理的商品        
        $goods->where('admin_id',$this->adminData['id']);
        //根据商品的id查询数据
        $goods->where('id',$goods_id);
        $item = $goods->find();
        if(!$item){
            $this->error('无效的商品ID！');
        } 
        //逻辑删除，只需要改变is_delete字段的值为1即可
        $is = $item->save(['is_delete' => 1]);
        if($is){
            $this->success('删除商品成功！');
        }        
    }
} 